package Servlets.yz;

import Bean.Contract;
import Bean.supplier;
import Util.GokJDBCUtil2;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.time.LocalDateTime;

@WebServlet(name="ContractChangeServlet",urlPatterns = "/ContractChangeServlet")
public class ContractChangeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");

        if("updateBefore".equals(method)){//更新之前，获取原供应商信息
            String id = req.getParameter("id");
            String sql = "select * from contract where id=?";
            Contract contract = GokJDBCUtil2.queryForObject(sql, Contract.class, id);
            req.setAttribute("contract",contract);
            req.getRequestDispatcher("pages/contractUpdate.jsp").forward(req,resp);
        }
        else if("update".equals(method)){//结合更新之间的数据，进行相应更新
            String id = req.getParameter("id");
            String contractname = req.getParameter("contractname");
            String documentpath = req.getParameter("documentpath");
            String supplierid = req.getParameter("supplierid");
            String startdate = req.getParameter("startdate");
            String enddate = req.getParameter("enddate");
            LocalDateTime updateTime = LocalDateTime.now();
            String sql = "update  contract set contractname=?,documentpath=?,supplierid=?,startdate=?,enddate=?,updatetime=? where id=?";
            GokJDBCUtil2.update(sql,contractname,documentpath,supplierid,startdate,enddate,updateTime,id);
            resp.sendRedirect(req.getContextPath()+"/pages/contractUpdateSuccess.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
